package controller.transform;

import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import math.Angles;

import component.AbstractComponent;
import component.Ball;
import component.Tube;

public class InitMode extends TransformMode {

	public InitMode(Gui gui, Scene scene) {
		super(gui, scene);
		gui.unlockCamera();
		scene.resetState();
		setText();
	}
	
	private void setText() {
		switch (scene.getMode()) {
		case TransformPanel.ALIGN_TUBES_MODE:
		case TransformPanel.ALIGN_TUBE_XY_MODE:
		case TransformPanel.ALIGN_TUBE_XZ_MODE:
		case TransformPanel.ALIGN_TUBE_YZ_MODE:	
			gui.setStatusText("Select a pivot for rotation.");
			break;
		default:
			gui.setStatusText("Select a pivot or an axis for rotation.");			
			break;
		}
	}

	@Override
	public TransformMode mousePressed(MouseEvent e) {
		if (e.getButton() == MouseEvent.BUTTON3)
			return this;
		
		AbstractComponent component = scene.pickClosest(e);
		if (component == null)
			return this;
		
		if (component instanceof Ball) {
			Ball ball = (Ball)component;
			return ballSelected(ball);
		} else if (component instanceof Tube) {
			Tube tube = (Tube)component;
			return tubeSelected(tube);
		}
		
		return this;
	}
	
	private TransformMode ballSelected(Ball ball) {
		switch (scene.getMode()) {
		case TransformPanel.ROTATION_MODE:
			return new RotatePivotMode(gui, scene, ball);
		case TransformPanel.ALIGN_TUBE_XY_MODE:
			return new AlignToXY(gui, scene, ball);
		case TransformPanel.ALIGN_TUBE_XZ_MODE:
			return new AlignToXZ(gui, scene, ball);
		case TransformPanel.ALIGN_TUBE_YZ_MODE:
			return new AlignToYZ(gui, scene, ball);
		case TransformPanel.ALIGN_TUBES_MODE:
			return new AlignToTube(gui, scene, ball);
		case TransformPanel.MAKE_TRIANGLE:
			return new AlignAnglePivot(gui, scene, ball, Angles.getAngle(3));
		case TransformPanel.MAKE_SQUARE:
			return new AlignAnglePivot(gui, scene, ball, Angles.getAngle(4));
		case TransformPanel.MAKE_PENTAGON:
			return new AlignAnglePivot(gui, scene, ball, Angles.getAngle(5));
		case TransformPanel.MAKE_HEXAGON:
			return new AlignAnglePivot(gui, scene, ball, Angles.getAngle(6));
		default:
			if (scene.getMode() >= TransformPanel.MAKE_7) {
				int n = scene.getMode() - TransformPanel.MAKE_7 + 7;
				return new AlignAnglePivot(gui, scene, ball, Angles.getAngle(n));
			}
		}
		return this;
	}
	
	private TransformMode tubeSelected(Tube tube) {
		switch (scene.getMode()) {
		case TransformPanel.ROTATION_MODE:
			return new RotateAxisMode(gui, scene, tube);
		case TransformPanel.MAKE_TRIANGLE:
			return new AlignAngleAxis(gui, scene, tube, Angles.getAngle(3));
		case TransformPanel.MAKE_SQUARE:
			return new AlignAngleAxis(gui, scene, tube, Angles.getAngle(4));
		case TransformPanel.MAKE_PENTAGON:
			return new AlignAngleAxis(gui, scene, tube, Angles.getAngle(5));
		case TransformPanel.MAKE_HEXAGON:
			return new AlignAngleAxis(gui, scene, tube, Angles.getAngle(6));
		default:
			if (scene.getMode() >= TransformPanel.MAKE_7) {
				int n = scene.getMode() - TransformPanel.MAKE_7 + 7;
				return new AlignAngleAxis(gui, scene, tube, Angles.getAngle(n));
			}

		}
		return this;
	}

	@Override
	public TransformMode mouseReleased(MouseEvent e) {
		return this;
	}

	@Override
	public TransformMode mouseMoved(MouseEvent e) {
		return this;
	}

	@Override
	public TransformMode mouseDragged(MouseEvent e) {
		return this;
	}

	@Override
	public TransformMode keyPressed(KeyEvent e) {
		return this;
	}

	@Override
	public TransformMode keyReleased(KeyEvent e) {
		return this;
	}

	@Override
	public void deselected() {
		
	}


}
